package com.wlhse.listener;

import com.wlhse.dao.MessageDao;
import com.wlhse.entity.Mail;
import com.wlhse.util.state_code.CodeDict;
import com.wlhse.util.state_code.NR;
import com.wlhse.websocket.MyWebSocketHandler;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.socket.TextMessage;

import javax.annotation.Resource;

public class QueueListener2 {
    MyWebSocketHandler handler = new MyWebSocketHandler();

    @Resource
    private MessageDao messageDao;

    @Transactional
    public void displayMail(Mail mail) throws Exception {
        //存入数据库
        messageDao.addAccidentRecord(mail);
        //存入缓存(缓存存与谁聊过天，一周的聊天内容)
        //返回json内容  发送人json信息---接收人json信息---信息json内容(list集合)
        String newStringRetrun = NR.r(CodeDict.CODE_MESSAGE_DATA, 1, 0, mail, null, 0, 0);
        //发送给在线用户
        handler.sendMessageToUser((long) mail.getToUserId(), new TextMessage(newStringRetrun));
    }
}
